#include "testWin.h"
#include "mainWin.h"
#include "gl.h"

#include <windows.h>

window* tstwin = NULL;

static LRESULT CALLBACK testProc(HWND,UINT,WPARAM,LPARAM);

testWin::testWin():window(){
    callback = (WNDPROC)testProc;
    dwStyle = WS_POPUP;
    dwExStyle = DFT_WIN_EX_STYLE|WS_EX_LAYERED;
    className = "testWin";
    name = "SplashScreen";
    width = 100;
    height = 30;
    xpos = (GetSystemMetrics(SM_CXSCREEN)-width)/2;
    ypos = (GetSystemMetrics(SM_CYSCREEN)-height)/2;

    create();

    setOpacity(0);

    rm.add(tft = new font());
    tft->setValidity(RES_ESSENTIAL);
    tft->setSize(20);
    tft->setBold(true);
    tft->create();

    tim.setDuration(2*CLOCKS_PER_SEC);
}

testWin::~testWin(){}

font* testWin::getFont(){
    setCurrent();
    return tft;
}

void        testWin::drawGLScene(){
    window::drawGLScene();

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);			// Clear The Screen And The Depth Buffer
	glLoadIdentity();							// Reset The Current Modelview Matrix

    glColor3f(0.0f,1.0f,0.0f);
    glRasterPos(5,7);
    glPrint(tft,"Splash !!!");

    if( !tim.isRunning() ){
        tim.start();
    }else{
        setOpacity(tim.getProgress()*2);
    }
	if( tim.isEnded() ){
        tstwin->destroy();
        delete tstwin;
        tstwin = NULL;
        win = new mainWin();
	}

}

bool testWin::initGL(){

	glShadeModel(GL_SMOOTH);						// Enables Smooth Shading
	glClearColor(0.0f, 0.0f, 1.0f, 0.0f);					// Black Background

	glClearDepth(1.0f);							// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);						// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);							// The Type Of Depth Test To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);			// Really Nice Perspective Calculations

	return true;								// Initialization Went OK

}

static LRESULT CALLBACK testProc(	HWND	hWnd,					// Handle For This Window
				UINT	uMsg,					// Message For This Window
				WPARAM	wParam,					// Additional Message Information
				LPARAM	lParam)					// Additional Message Information
{
    if( !tstwin ) return DefWindowProc(hWnd,uMsg,wParam,lParam);
	switch (uMsg)								// Check For Windows Messages
	{
		case WM_ACTIVATE:						// Watch For Window Activate Message
		{
			if (!HIWORD(wParam))					// Check Minimization State
			{
				tstwin->setActive(true);					// Program Is Active
			}
			else
			{
				tstwin->setActive(false);					// Program Is No Longer Active
			}

			return 0;						// Return To The Message Loop
		}
		case WM_SYSCOMMAND:						// Intercept System Commands
		{
			switch (wParam)						// Check System Calls
			{
				case SC_SCREENSAVE:				// Screensaver Trying To Start?
				case SC_MONITORPOWER:				// Monitor Trying To Enter Powersave?
				return 0;					// Prevent From Happening
			}
			break;							// Exit
		}
		case WM_CLOSE:							// Did We Receive A Close Message?
		{
			PostQuitMessage(0);					// Send A Quit Message
			return 0;						// Jump Back
		}
		case WM_SIZE:							// Resize The OpenGL Window
		{
			tstwin->resizeGLScene(LOWORD(lParam),HIWORD(lParam));
			return 0;						// Jump Back
		}
	}

	if( !tstwin->getInput()->translateInputMsg(uMsg,wParam,lParam) ) return 0;

	// Pass All Unhandled Messages To DefWindowProc
	return DefWindowProc(hWnd,uMsg,wParam,lParam);
}


